<template>
  <div class="grid-container">
    <div class="grid-item" v-for="item in dataList" :key="item.infoId">
      <div class="car-row">
        <!-- 车辆名称 -->
        <div class="car-name">
          {{ item.brandName }}  &nbsp;  {{ item.modelName }}
          <div>
            &nbsp;&nbsp;  {{ item.styleName }}
          </div>
        </div>
        <!-- 状态标签 -->
        <div
            class="labels"
            :style="{backgroundColor: item.color}"
            v-if="item.labelName"
        >
          {{ item.labelName }}
        </div>
      </div>
      <!-- 车辆图片 -->
      <div class="car-image">
        <el-image
            :src="formatImageUrl(item.pictures)"
            :preview-src-list="[formatImageUrl(item.pictures)]"
            preview-teleported
            fit="contain"
            lazy
        >
          <template #error>
            <div class="image-placeholder">
              <el-icon><Picture /></el-icon>
            </div>
          </template>
        </el-image>
      </div>

      <!-- 车辆价格信息 -->
      <div class="car-price-info">
        <div class="price-item">
          收车价&nbsp;{{ item.costOfCollection }}￥ &nbsp;&nbsp; 售卖价&nbsp;{{ item.sellingPrice }}￥
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">



// 接收父组件传递的参数
const props = defineProps({
  dataList: {
    type: Array,
    default: () => []
  }
});

// 图片URL格式化
const formatImageUrl = (url: string) => {
  if (!url) return '';
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url;
  }
  const baseURL = import.meta.env.VITE_IMAGE_BASE_URL || import.meta.env.VITE_ADMIN_PROXY_PATH || '';
  return baseURL + (url.startsWith('/') ? url : '/' + url);
};
</script>

<style scoped>
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
  padding: 10px 0;
}

.grid-item {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  background-color: #fff;
  width: 270px;
  box-sizing: border-box;
  text-align: center;
}

.grid-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}
.car-row {
  display: flex;
  align-items: center;
  margin: 10px 15px;
}

.car-name {
  height: 42px;
  font-size: 14px;
  font-weight: 600;
  text-align: left;
  flex: 1;
}

.labels {
  padding: 5px 10px;
  color: #000000;
  font-size: 12px;
  border-radius: 2px;
  min-width: 50px;
  text-align: center;
}
.car-image {
  position: relative;
  height: 150px;
  width: 100%;
  border: 1px solid #ddd; /* 添加边框使图片大小一致 */
  overflow: hidden; /* 防止图片超出容器 */
  display: flex;
  align-items: center;
  justify-content: center;
}

.car-image .el-image {
  max-width: 90%;  /* 控制图片最大宽度 */
  max-height: 90%; /* 控制图片最大高度 */
}

.image-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
}

.car-price-info {
  padding: 10px 0;
}

.price-item {
  margin: 5px 0;
  font-size: 14px;
}
</style>
